<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="stationFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="工位名称" prop="stationName">
        <el-input v-model="drawerProps.row!.stationName" placeholder="请填写工位名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="工位编号" prop="stationNo">
        <el-input v-model="drawerProps.row!.stationNo" placeholder="请填写工位编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属装置" prop="deviceId">
        <el-select v-model="drawerProps.row!.deviceId" placeholder="请选择所属装置" clearable>
          <el-option v-for="item in deviceSelList" :key="item.deviceId" :label="item.deviceName" :value="item.deviceId" />
        </el-select>
      </el-form-item>
      <el-form-item label="工位类型" prop="stationType">
        <el-select v-model="drawerProps.row!.stationType" placeholder="请选择工位类型" clearable>
          <el-option v-for="item in stationType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="通讯地址" prop="stationAddress">
        <el-input v-model="drawerProps.row!.stationAddress" placeholder="请填写通讯地址" clearable></el-input>
      </el-form-item>

      <el-form-item label="电源地址" prop="powerAddress">
        <el-input v-model="drawerProps.row!.powerAddress" placeholder="请填写电源地址" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="StationDrawer">
import { ref, reactive } from "vue";
import { stationType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { Station, ResultData } from "@/api/interface";
import { getCabinetSelList } from "@/api/modules/files1Manage/cabinet";
const validateNumber = (rule: any, value: string, callback: (arg0?: Error | undefined) => void) => {
  if (!/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
    callback(new Error("请输入数字值"));
  } else {
    callback();
  }
};
const rules = reactive({
  stationName: [{ required: true, trigger: "blur", message: "请填写工位名称" }],
  stationNo: [
    { required: true, trigger: "blur", message: "请填写工位编号" },
    { validator: validateNumber, trigger: "blur", message: "工号必须为数字" }
  ],
  deviceId: [{ required: true, trigger: "blur", message: "请选择所属装置" }],
  stationType: [{ required: true, trigger: "blur", message: "请选择工位类型" }],
  stationAddress: [
    { required: true, trigger: "blur", message: "请填写通讯地址" },
    { validator: validateNumber, trigger: "blur", message: "通讯地址必须由数字组成" }
  ],
  powerAddress: [{ required: true, trigger: "blur", message: "请填写电源地址" }]
});

const deviceSelList: any = ref([]);

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Station.ResStationInfoList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  const { result } = (await getCabinetSelList({ pageNum: 1, pageSize: 100000000 })) as ResultData;
  deviceSelList.value = result.dataList;
};

// 提交数据（新增/编辑）
const stationFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  stationFormRef.value && stationFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  stationFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!({ ...drawerProps.value.row });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
defineExpose({
  acceptParams
});
</script>
